﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace MainFrame
{
    public class SerializeHelper
    {
        public static bool isDisplayInfo=true;

        /// <summary>
        /// 信息提示（每个帮助类都加这个，是为了引用单个类和引用多个类时，能共用）
        /// 这种由于有多个判断，会增加时间开销，如果你确定了用哪种显示方式
        /// 且不会再更改，可把本方法注释掉，把下面注释掉的方法启用
        /// </summary>
        /// <param name="chinese">中文提示</param>
        /// <param name="english">英文提示</param>
        //protected static void  displalyInfo.displayInfoMain?.Invoke(string chinese, string english)
        //{
        //    if (isDisplayInfo)
        //    {
        //        if (InfomationDisplay.isQueue_info)
        //        {
        //            if (InfomationDisplay.isChinese_info)
        //            {
        //                InfomationDisplay.bc_message_list.Add(chinese);
        //            }
        //            else
        //            {
        //                InfomationDisplay.bc_message_list.Add(english);
        //            }
        //        }
        //        else
        //        {
        //            InfomationDisplay.Display?.Invoke(chinese, english);
        //        }
        //    }
        //    else
        //    {
        //        LogNetHelper.WriteLog(chinese);
        //    }
        //}
        /// <summary>
        /// 直接显示
        /// </summary>
        /// <param name="chinese"></param>
        /// <param name="english"></param>
        //private void  displalyInfo.displayInfoMain?.Invoke(string chinese, string english)
        //{
        //    if (isDisplayInfo)
        //    {
        //        InfoDisplay.Display?.Invoke(chinese, english);
        //    }
        //    else
        //    {
        //        LogNetHelper.WriteLog(chinese);
        //    }
        //}
        public static object DeserializeFile()
        {
            try
            {
                object obj = new object();
                string CustomSetupPath = "CustomConfig.par";
                using (FileStream fs = new FileStream(CustomSetupPath, FileMode.Open))
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    obj = bf.Deserialize(fs);
                    displalyInfo.displayInfoMain?.Invoke("用户设置 加载成功,user settings successfully loaded");
                    return obj;
                }              
              
            }
            catch (System.Exception ex)
            {
                 displalyInfo.displayInfoMain?.Invoke("用户设置 加载失败：,user settings load failed ：" + ex.Message);
                return null;
            }
        }

        public static object DeserializeFile(string path)
        {
            try
            {
                if (!File.Exists(path))
                {
                     displalyInfo.displayInfoMain?.Invoke(" 参数配置文件不存, parameter file did not exist");
                    return null;
                }
                object obj = new object();
                using (FileStream fs = new FileStream(path, FileMode.Open))
                {
                    BinaryFormatter bf = new BinaryFormatter();

                    obj = bf.Deserialize(fs);
                     displalyInfo.displayInfoMain?.Invoke("用户设置 加载成功，user settings successfully loaded");
                    return obj;
                }

             
            }
            catch (System.Exception ex)
            {
                 displalyInfo.displayInfoMain?.Invoke($"用户设置 加载失败：，user settings load failed ：{ex}");
              
                return null;
            }
        }

        public static object DeserializeFile(string path,string note)
        {
            try
            {
                if (!File.Exists(path))
                {
                     displalyInfo.displayInfoMain?.Invoke(note + " 参数配置文件不存, parameter file did not exist");
                    return null;
                }
                object obj = new object();
                using (FileStream fs = new FileStream(path, FileMode.Open))
                {
                    BinaryFormatter bf = new BinaryFormatter();

                    obj = bf.Deserialize(fs);
                     displalyInfo.displayInfoMain?.Invoke(note+" 加载成功, loaded successfully");
                    return obj;
                }


            }
            catch (System.Exception ex)
            {
                 displalyInfo.displayInfoMain?.Invoke(note +$" 加载失败：, load failed：{ex}");
                return null;
            }
        }
        public static void serializeFile(object obj)
        {
            try
            {
                string CustomSetupPath = "CustomConfig.par";
                if (File.Exists(CustomSetupPath))
                {
                    File.Delete(CustomSetupPath);
                }
                using (FileStream fs = new FileStream(CustomSetupPath, FileMode.Create))
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    bf.Serialize(fs, obj);
                }
                 displalyInfo.displayInfoMain?.Invoke("用户设置保存成功，user settings saved successfully");
 
            }
            catch (System.Exception ex)
            {
                 displalyInfo.displayInfoMain?.Invoke($"用户设置保存失败：，user settings save failed：{ex}"); 
            }
        }
        public static void serializeFile(string path,object obj)
        {
            try
            {
                if (!Directory.Exists(Path.GetDirectoryName(path)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(path));
                }
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                using (FileStream fs = new FileStream(path, FileMode.Create))
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    bf.Serialize(fs, obj);
                }
                // displalyInfo.displayInfoMain?.Invoke("参数保存成功", "parameter saved successfully");
            }
            catch (System.Exception ex)
            {
                 displalyInfo.displayInfoMain?.Invoke($"参数序列化失败：，parameter save failed：{ex}" );
             
            }
        }

        public static void serializeFile(string path, object obj,string note)
        {
            try
            {
                if (!Directory.Exists(Path.GetDirectoryName(path)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(path));
                }
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                using (FileStream fs = new FileStream(path, FileMode.Create))
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    bf.Serialize(fs, obj);
                }
                 displalyInfo.displayInfoMain?.Invoke(note+ "保存成功，saved successfully");
            }
            catch (System.Exception ex)
            {
                 displalyInfo.displayInfoMain?.Invoke(note +$"保存失败，save failed：{ex}");
            }
        }
    }
}
